<?php

/**
 * General Shopalong functions
 *
 * @author Stuart Metcalfe <shoopdev@googlemail.com>
 * @version 1.0
 *
 * Copyright (c) 2006 Shopalong
 *
 * Released under the GNU General Public License
 */

/**
 * Function to get the users IP address
 *
 * @return string I.P. Address
 */

function sal_getIpAddress()
{
	if (isset($_SERVER))
	{
		if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
		{
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		
		elseif (isset($_SERVER['HTTP_CLIENT_IP']))
		{
			$ip = $_SERVER['HTTP_CLIENT_IP'];
		}
		
		else
		{
			$ip = $_SERVER['REMOTE_ADDR'];
		}
	}
	
	else
	{
		if (getenv('HTTP_X_FORWARDED_FOR'))
		{
			$ip = getenv('HTTP_X_FORWARDED_FOR');
		}
		
		elseif (getenv('HTTP_CLIENT_IP'))
		{
			$ip = getenv('HTTP_CLIENT_IP');
		}
		
		else
		{
			$ip = getenv('REMOTE_ADDR');
		}
	}
	
	return $ip;
}

/**
 * Function to get the available zones for the requested country
 *
 * @param integer $countryId
 * @return array
 */

function sal_getZones($countryId)
{
	$query = 'select zone_name from %s where zone_country_id = "%d" order by zone_name';
	$query = sprintf($query, TABLE_ZONES, $countryId);
	$zones_query = tep_db_query($query);
	$zones_array = array();
	
	while ($zones_values = tep_db_fetch_array($zones_query)) {
		$zones_array[$zones_values['zone_name']] = $zones_values['zone_name'];
	}
	
	return $zones_array;
}

/**
 * Function to load and initialize the plugins in DIR_FS_PLUGINS
 *
 * This function is called in application_top.php and does not usually need to
 * be called again during the running of the script.
 */

function sal_loadPlugins()
{
	$handle = opendir(DIR_FS_PLUGINS);
	
	if (!$handle)
	{
		return;
	}
	
	while (false !== ($identifier = readdir($handle)))
	{
		if (ereg('^([A-Za-z0-9]+)\.([A-Za-z0-9]+)$', $identifier) && is_dir(DIR_FS_PLUGINS.$identifier))
		{
			$classes = get_declared_classes();
			$classesSize = sizeof($classes);
			$classFile = DIR_FS_PLUGINS.$identifier.'/plugin.php';
			
			if (!file_exists($classFile))
			{
				next;
			}
			
			include_once($classFile);
			$classes = get_declared_classes();
			$newClassesSize = sizeof($classes);
			
			if ($newClassesSize <= $classesSize)
			{
				next;
			}
			
			$indexOffset = $newClassesSize - $classesSize;
			
			while ($indexOffset > 0)
			{
				$newClass = $classes[$newClassesSize - $indexOffset--];
				$object = new $newClass();
			}
		}
	}
	
	closedir($handle);
}

?>
